<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物业名称">
        <el-select v-model="queryParams.pmid" @visible-change="prosList" clearable>
          <el-option v-for="item in this.properList" :key="item.pmid" :label="item.propertyName"
                     :value="item.pmid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车场名称">
        <el-select v-model="queryParams.chid" @visible-change="carsList" clearable>
          <el-option v-for="item in this.carList" :key="item.chid" :label="item.chname" :value="item.chid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车辆类型" >
        <el-select v-model="queryParams.ctid" clearable :disabled="queryParams.chid === null || queryParams.chid === '' || queryParams.chid === undefined">
          <el-option v-for="item in this.carTList" :key="item.carsType.ctid" :label="item.carsType.typeName" :value="item.carsType.ctid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="licenseNumber">
        <el-input
          v-model="queryParams.licenseNumber"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主电话" prop="carsTel">
        <el-input
          v-model="queryParams.carsTel"
          placeholder="请输入业主电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['info:cars_info:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['info:cars_info:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['info:cars_info:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['info:cars_info:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cars_infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="cmid" width="80"/>
      <el-table-column label="是否删除" align="center" prop="deleted" width="80"/>
      <el-table-column label="车牌号" align="center" prop="licenseNumber" width="150"/>
      <el-table-column label="车辆类型" align="center" prop="carsType.typeName" width="100"/>
      <el-table-column label="车牌颜色" align="center" prop="carsPlateColors.carsPlateName" width="100"/>
      <el-table-column label="所属物业" align="center" prop="propertyManagement.propertyName" width="150"/>
      <el-table-column label="业主电话" align="center" prop="ownerMsg.ownerTel" width="200"/>
      <el-table-column label="业主姓名" align="center" prop="ownerMsg.ownerName" width="100"/>
      <el-table-column label="停车场" align="center" prop="carsHome.chname" width="100"/>
      <el-table-column label="备注" align="center" prop="remark" width="200"/>
      <el-table-column label="关联编号" align="center" prop="carsHomeParking.chpid" width="80"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="300px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLookOwner(scope.row)"
            v-hasPermi="['info:cars_info:edit']"
          >查看业主
          </el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['info:cars_info:edit']"-->
<!--          >修改记录-->
<!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="$router.push({path:'/carss/cars',query:{licenseNumber: scope.row.licenseNumber}})"
            v-hasPermi="['info:cars_info:edit']"
          >停车记录
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['info:cars_info:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['info:cars_info:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px" :disabled="true">
        <el-form-item label="物业名称" prop="code">
          <el-input v-model="form.propertyManagement.propertyName"/>
        </el-form-item>
        <el-form-item label="所属停车场" prop="deleted">
          <el-input v-model="form.carsHome.chname"/>
        </el-form-item>
        <el-form-item label="车牌号" prop="licenseNumber">
          <el-input v-model="form.licenseNumber"/>
        </el-form-item>
        <el-form-item v-if="form.ownerMsg.omid === null || form.ownerMsg.omid === '' ">
          <div style="width: 60%;margin: 30px auto"><img src="../../../../public/images/nullUser.png" alt="空用户图片"/></div>
        </el-form-item>
        <template v-else>
          <el-form-item label="物业名称" prop="code">
            <el-input v-model="form.ownerMsg.ownerName"/>
          </el-form-item>
          <el-form-item label="所属停车场" prop="deleted">
            <el-input v-model="form.ownerMsg.ownerTel"/>
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCars_info, getCars_info, delCars_info, addCars_info, updateCars_info} from "@/api/info/cars_info";
import {listHome, listProperty, listOwner_info, listColors} from "@/api/ruoyi-home/home";
import {listType, getType, delType, addType, updateType} from "@/api/info/type";
import {listCt, getCt, delCt, addCt, updateCt} from "@/api/info/ct";

export default {
  name: "Cars_info",
  data() {
    return {
      // chidisNull: this.queryParams.chid === null ? true : false,
      showUserList: false,
      selectedUser: null,
      form: {
        propertyManagement: {},
        carsHome: {},
        ownerMsg: {},
        carsType: {},
        carsPlateColors: {},
      },
      properList: [],
      carList: [],
      carTList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPropertyManagement: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息表格数据
      cars_infoList: [],
      // 物业管理表格数据
      propertyManagementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        status: null,
        deleted: null,
        licenseNumber: null,
        ctid: null,
        lpcid: null,
        pmid: null,
        carsTel: null,
        omid: null,
        chid: null,
        remark: null,
      },
    };
  },
  created() {
    this.getList();
    this.prosList();
    this.carsList();
  },
  methods: {
    handleLookOwner(row) {
      // console.log(row)
      this.open = true
      this.form = row;
    },
    prosList() {
      listProperty().then(res => {
        this.properList = res.rows;
        this.queryParams.chid = null;
        this.queryParams.ctid = null;
      })
    },
    carsList() {
      listHome(this.queryParams).then(res => {
        this.carList = res.rows;
        this.queryParams.ctid = null;
        if (this.queryParams.chid !== null || this.queryParams.chid !== '' || this.queryParams.chid !== undefined) {
          this.carsType();
        }
      })
    },
    carsType() {
      listCt(this.queryParams).then(res=>{
        this.carTList = res.rows;
      })
    },
    /** 查询车辆信息列表 */
    getList() {
      this.loading = true;
      listCars_info(this.queryParams).then(response => {
        // console.log(response.rows)
        this.cars_infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cmid: null,
        code: null,
        status: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        licenseNumber: null,
        ctid: null,
        lpcid: null,
        pmid: null,
        carsTel: null,
        omid: null,
        chid: null,
        propertyManagement: {
          pmid: null,
          propertyName: null,
        },
        carsHome: {
          chid: null,
          chname: null,
        },
        ownerMsg: {
          omid: null,
          ownerName: null,
          ownerTel: null,
        },
        carsType: {
          ctid: null,
          typeName: null,
        },
        carsPlateColors: {
          lpcid: null,
          carsPlateName: null,
        },
      };
      this.propertyManagementList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cmid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path: 'addcars'})
      // this.reset();
      // this.open = true;
      this.title = "添加车辆信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({path: 'upcars', query: {cmid: row.cmid || this.ids}});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cmids = row.cmid || this.ids;
      this.$modal.confirm('是否确认删除车辆信息编号为"' + cmids + '"的数据项？').then(function () {
        return delCars_info(cmids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 物业管理序号 */
    rowPropertyManagementIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 复选框选中数据 */
    handlePropertyManagementSelectionChange(selection) {
      this.checkedPropertyManagement = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sys-cars-info/cars_info/export', {
        ...this.queryParams
      }, `cars_info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
